package com.zjdiepu.www.proxy.biz.impl.own;

import java.io.File;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.config.FileProxyConfig;
import com.zjdiepu.www.proxy.biz.own.HzBiz;
import com.zjdiepu.www.proxy.dao.own.HzDao;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferOtherAttachments;

@Service
public class HzBizImpl implements HzBiz{

	private final Logger logger = LoggerFactory.getLogger(HzBizImpl.class);
	
	@Autowired
	private HzDao hzDao;
	
	@Autowired
	protected FileProxyConfig fileProxyConfig;
	
	@Override
	public List<Map<String, String>> queryWhereSql(String sqlXmls) {
		
		return hzDao.queryWhereSql(sqlXmls);
	}

	@Override
	public int countSqlXmls(String sqlXmls) {
		
		return hzDao.countSqlXmls(sqlXmls);
	}

	@Override
	public Map<String, String> selectMapsSql(String sqlXmls) {
		
		return hzDao.selectMapsSql(sqlXmls);
	}

	@Override
	public void loggerError(String errMessage) {
		
		String error  = "zpp "+errMessage;
		logger.error(error);
	}

	@Override
	public void loggerInfo(String errMessage) {
		
		String info  = "zpp "+errMessage;
		logger.info(info);
	}

	@Override
	public void loggerDebug(String errMessage) {
		
		String debug  = "zpp "+errMessage;
		logger.debug(debug);
	}

	@Override
	public boolean checkCompleted(TransferOtherAttachments transferOtherAttachments) {
		if(null == transferOtherAttachments) {
			return false;
		}
		String localFilePath = transferOtherAttachments.getLocalFilePath();
		if(StringUtils.isBlank(localFilePath)) {
			return false;
		}
		String filePath = fileProxyConfig.getBasePathNginx() + transferOtherAttachments.getLocalFilePath();
		if(!new File(filePath).exists()) {
			return false;
		}
		return true;
	}

	@Override
	public String retcode(Object object) throws Exception {
		if(null == object)
		{
			return "";
		}
		JSONObject json = JSONObject.fromObject(object);
		String jsonString = json.get("signBlock").toString();
		json = JSONObject.fromObject(jsonString);
		json = JSONObject.fromObject(json.get("pub"));
		return json.getString("retcode");
	}
	
	@Override
	public String retmsg(Object object) {
		if(null == object) {
			return null;
		}
		JSONObject json = JSONObject.fromObject(object);
		String jsonString = json.get("signBlock").toString();
		json = JSONObject.fromObject(jsonString);
		json = JSONObject.fromObject(json.get("pub"));
		return json.getString("retmsg");
	}
}
